<?php
class MyLibrary_AclPlugin extends Zend_Controller_Plugin_Abstract{
    protected $_acl;
    
    public function __construct($acl){
        $this->_acl = $acl;
    }
    
    public function preDispatch(Zend_Controller_Request_Abstract $request){
        try{
            $controllerName = $request->getControllerName();
            $actionName = $request->getActionName();
            $moduleName = $request->getModuleName();
            
            $auth = Zend_Auth::getInstance();
            if($auth->hasIdentity()){
                $role = $auth->getIdentity()->user_role;
                
                if(!$this->_acl->isAllowed($role, $moduleName. ':' .$controllerName, $actionName)){
                        $request->setControllerName('authentication')
                        ->setActionName('message');
                }
            }
            else{
                if($moduleName == 'admin'){
                    $request->setControllerName('auth')
                            ->setActionName('login')
                            ->setModuleName('admin');
                }
                
            }
        }
        catch (Zend_Exception $ex){
            echo $ex;
        }
       
    }
}